﻿using System;
using System.Linq;
using System.Reflection;
using SR.ExpressionEvaluator.Lib.Core;

namespace SR.ExpressionEvaluator.Lib
{
    public static class Factory
    {
        private static Evaluator evaluator = new Evaluator();

        public static IEvaluator GetEvaluator()
        {
            return (IEvaluator)evaluator;
        }

        public static IFunction GetFunction(string name)
        {
            var q = (from t in Assembly.GetExecutingAssembly().GetTypes()
                     where t.IsClass 
                        && t.Namespace == "SR.ExpressionEvaluator.Lib.Core.Functions" 
                        && t.GetInterfaces().Contains(typeof(IFunction))
                        && t.Name.ToUpper() == name.ToUpper()
                     select t).FirstOrDefault();

            if (q != null)
            {
                return (IFunction)Activator.CreateInstance(q);
            }
            throw new Exception("Factory: Unknown function");
        }
    }
}
